package com.vertx.zipkin;

import com.vertx.util.Runner;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.http.HttpServer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.tracing.zipkin.ZipkinTracingOptions;

public class HelloVerticle extends AbstractVerticle {
	public static void main(String[] args) {

		Runner.runExample(HelloVerticle.class);

	}

	public void HelloVerticle(){

	}

	@Override
	public void start(Promise<Void> startPromise) throws Exception {

		HttpServer server = vertx.createHttpServer();


		Router router = Router.router(vertx);

		router.route().handler(BodyHandler.create());

		router.route("/hello").handler(req -> {
			WebClient client = WebClient.create(vertx);
			client.get(8082, "localhost", "/world")
					.send()
					.onSuccess(response -> {
						String rs = response.bodyAsString();
						req.response()
								.putHeader("content-type", "application/json")
								.end(new JsonObject().put("return", "hello" + rs).toString());
					})
					.onFailure(err -> {
						System.out.println("fail:" + err.getMessage());
					});

		});
		server.requestHandler(router).listen(8080);
	}
}